﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAOTAgency;

namespace TravelAgency
{
    public partial class WebFormReservation : System.Web.UI.Page
    {
        private static List<Customer> attenders = new List<Customer>();

        private void CalculateTotalPrice()
        {
            int tourId = int.Parse(Request.Params["TourID"]);
            Tour tour = DAO.GetTourById(tourId).First();
            if (attenders.Count == 0)
            {
                LabelTotalPrice.Text = tour.Price.ToString();
            }
            else
            {
                decimal totalPrice = tour.Price * (attenders.Count + 1);
                LabelTotalPrice.Text = totalPrice.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            CalculateTotalPrice();
            DivSuccess.Visible = false;
        }

        protected void ButtonAddAttender_Click(object sender, EventArgs e)
        {
            Customer newCustomer = new Customer();
            newCustomer.FirstName = TextBoxFirstNameAtt.Text;
            newCustomer.LastName = TextBoxLastNameAtt.Text;
            newCustomer.Passport = TextBoxPassportAtt.Text;
            attenders.Add(newCustomer);
            CalculateTotalPrice();
            if (attenders.Count == 4)
            {
                ButtonAddAttender.Enabled = false;
            }

            TextBoxFirstNameAtt.Text = string.Empty;
            TextBoxLastNameAtt.Text = string.Empty;
            TextBoxPassportAtt.Text = string.Empty;
            int numberOfAttenders = int.Parse(LabelNumerOfAttenders.Text);
            LabelNumerOfAttenders.Text = (numberOfAttenders + 1).ToString();
        }

        protected void ButtonMakeReservation_Click(object sender, EventArgs e)
        {
            Page.Validate("Guarantee");
            if (Page.IsValid)
            {
                Customer guarantee = new Customer();
                guarantee.FirstName = GuaranteeFirstName.Text;
                guarantee.LastName = GuaranteeLastName.Text;
                guarantee.Passport = GuaranteePassportNumber.Text;

                int tourId = int.Parse(Request.Params["TourID"]);

                int reservationId = DAO.InsertReservation(tourId, 0, attenders.Count);
                int guaranteeId = DAO.InsertCustomer(guarantee, reservationId);
                DAO.UpdateReservation(guaranteeId, reservationId);

                List<int> attendersIds = new List<int>();

                foreach (Customer item in attenders)
                {
                    attendersIds.Add(DAO.InsertCustomer(item, reservationId));
                }
                DivSuccess.Visible = true;
                DivReservations.Visible = false;
            }
        }

        protected void ButtonOK_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebFormMain.aspx");
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebFormMain.aspx");
        }
    }
}